package com.unisv.dj;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.os.PowerManager;

/**
 * <p>
 * </p>
 * 
 * @author fanzhen
 * @time 5:21:12 PM Sep 23, 2011
 * @email fanzhen.meng@gmail.com
 */
public class ServerService extends Service {
	protected PowerManager.WakeLock mWakeLock;
	public static final String TAG = "ServerService";
	public static final String COMMAND_COMING = "com.unisv.dj.ServerService.action.COMMAND_COMING";
	public static final String COMMAND_TAG = "com.unisv.dj.ServerService.msg.COMMAND_TAG";
	private static final String serverIpAddress = "192.168.0.188";
	private static final int serverPort = 8080;
	// private static final String serverIpAddress = "192.168.0.100";
	// private static final int serverPort = 11111;
	private Socket clientSocket = null;
	private InputStream in;
	private static OutputStream out;
	private static boolean isconneted = false;
	// private SendThread sendThread;
	private ReceiveThread receiveThread;

	Runnable connRun = new Runnable() {
		@Override
		public void run() {
			while (true) {
				if (isconneted){
						try {
							Thread.sleep(2*1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					continue;
				}
				try {
					InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
					clientSocket = new Socket(serverAddr, serverPort) {
						@Override
						public synchronized void close() throws IOException {
							isconneted = false;
							super.close();
						}
					};
					clientSocket.setKeepAlive(true);
					isconneted = true;
					out = clientSocket.getOutputStream();
					in = clientSocket.getInputStream();
					// sendThread = new SendThread();
					// sendThread.start();
					receiveThread = new ReceiveThread();
					receiveThread.start();
				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	};

	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		this.mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
		this.mWakeLock.acquire();
		Thread connThr = new Thread(connRun);
		connThr.start();
	}

	// class SendThread extends Thread{
	// @Override
	// public void run() {
	// while(true){
	// if (null != Command.command) {
	// try {
	// Log.i("command", "command..............");
	// out.flush();
	// out.write(Command.command);
	// Command.command = null;
	// } catch (Exception e) {
	// isconneted = false;
	// this.stop();
	// e.printStackTrace();
	// }
	// }
	// }
	// }
	// }
	class ReceiveThread extends Thread {
		@Override
		public void run() {
			try {
				byte[] msg = new byte[4];
				while (true) {
					in.read(msg, 0, 4);
					announceCommandComing(msg);
				}
			} catch (IOException e) {
				isconneted = false;
				this.stop();
				e.printStackTrace();
			}
		}
	}

	public static void doCommand(byte[] command) {
		if (null != command && isconneted) {
			try {
				out.write(command);
				out.flush();
			} catch (Exception e) {
				isconneted = false;
				e.printStackTrace();
			}
		}
	}

	private void announceCommandComing(byte[] msg) {
		Intent intent = new Intent(COMMAND_COMING);
		intent.putExtra(COMMAND_TAG, msg);
		sendBroadcast(intent);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		this.mWakeLock.release();
	}
}